<?php
/**
 * Description of SoBizMedia
 *
 * @author Alen
 * @since 1.0.0 2014-2-10 sutroon
 */
class SoBizMedia extends SoBizBase {
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->dm->tablename='tgeneraladvertisement';
    }

    /**
     * 读取一条记录
     * @param type $id
     * @param type $search
     * @since 1.0.0 2013-12-14
     */
    public function read_one($id, $search = 'ID=%d')
    {
        // 读取数据
        if ($id)
        {
            return $this->dm->select_one_row('', sprintf($search, $id));
        }
        return null;
    }
    
    /**
     * 
     * @return type
     * @since 1.0.0 2013-12-14
     */
    public function save(){        
        $this->dm->add_param('Title',request_string('title'),'标题不能为空');
        $this->dm->add_param('Content',request_string('content'),'内容不能为空');
        if($this->dm->has_error()) return $this->dm->get_error();
        $this->dm->add_param('ParentID',request_numeric('parent'));
        $img=upload_file('img');
        if($img) $this->dm->add_param('Picture',$img);
        $id=request_numeric('id');
        if($id>0){
            $this->dm->update('id='+$id);
        }else{
            $this->dm->insert();
        }
        return $this->dm->has_error()?$this->dm->get_error():'SUCCESS';                
    }

    /**
     * 删除一条记录
     * @return type
     * @since 1.0.0 2014-3-22 sutroon
     */
    public function remove()
    {
        $id = request_numeric('id');
        return $this->delete_one($id);
    }
    
    /**
     * 获取分页数据
     * @param type $pager_text
     * @param type $dataset
     * @param type $pageid
     * @param type $keyword
     * @since 1.0.0 2012-12-14 sutroon
     * @example 
     *  $pager_text='';
        $dataset=null;
        $biz->get_paging_data($pager_text, $dataset, request_numeric('page'), request_string('keyword'));
     */
    public function get_paging_data(&$pager_text,&$dataset,$pageid,$keyword){        
        // 获取分页数据
        $pager=new SoDataPager($pageid);        
        $sqlc=$keyword?' where Name like \'%'.$keyword.'%\' ':'';        
        $dataset=$this->dm->select_pagingset($pager->pageid,$pager->pagesize,$pager->recordcount,'tgeneraladvertisement where ParentID=0 '.$sqlc,'Ordinal, CreatedTime desc','ID, Title, Content, State, CreatedTime');
        $pager->set_dataset($dataset,$pager->recordcount);
        $pager_text=$pager->get_pager_text(set_url_param(array('page'=>'.pageid.')));
    }
}
